<?php

namespace app\admin\controller\shop;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="商家选择的平台商品")
 */
class PlatformGoods extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\ShopPlatformGoods();

    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {

            $page = input('page', 1, 'intval');
            $pageSize = input('page_size', 15, 'intval');

            $keyword = input('keyword', '', 'trim');

            $where = [];
            if ($this->user['role'] == 1) {
                $shopIds = \app\common\model\Shop::where('user_id', $this->user['id'])->column('id');
                if ($shopIds) {
                    $where[] = ['spg.shop_id', 'in', $shopIds];
                }
            }

            if ($keyword) {
                $where[] = ['g.title', 'like', '%' . $keyword . '%'];
            }

            $list = \app\common\model\ShopPlatformGoods::where($where)->alias('spg')
                ->leftJoin('goods g', 'g.id=spg.goods_id')->leftJoin('shop s','s.id=spg.shop_id');
            $totalCount = $list->count();

            $list = $list->order('spg.id desc')->page($page, $pageSize)
                ->field('spg.*,g.name as goods_title,g.show_price,g.img as goods_img,s.name as shop_name')->select()->toArray();


            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $totalCount,
                'data' => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $goodsId = explode(',',input('goods_id', '','trim'));
            $shopId = input('shop_id', 0,'intval');

            if (!$goodsId) {
                $this->error('请选择商品');
            }
            if(!$shopId){
                $this->error('请选择店铺');
            }

            $existGoodsId = \app\common\model\ShopPlatformGoods::where('shop_id',$shopId)->column('goods_id');

            $ids = array_diff($goodsId, $existGoodsId);

            if (!$ids) {
                $this->error('商品重复添加');
            }

            $insertData = [];
            foreach ($ids as $id) {
                $insertData[] = [
                    'shop_id' => $shopId,
                    'goods_id' => $id,
                    'create_time' => time()
                ];
            }

            $model = new \app\common\model\ShopPlatformGoods();
            $model->saveAll($insertData);

            $this->success('添加成功');
        }
        $shops = [];
        if ($this->user['role'] == 1) {
            $this->assign('is_shop', 1);
            $shops = \app\common\model\Shop::where('user_id', $this->user['id'])->where('status',1)->field('id,name')->select()->toArray();
        }else{
            $this->assign('is_shop', 0);
        }

        $this->assign('shops', $shops);
        return $this->fetch();

    }

    /**
     * @1NodeAnotation(title="删除")
     *//*
    public function delete()
    {

        $where = [];

        if (isset($this->user['shop_id'])) {
            $shopId = $this->user['shop_id'];
            $where[] = ['shop_id', '=', $shopId];
        } else {
            $shopId = input('shop_id', 0, 'intval');
            if (!$shopId) {
                $this->error('请指定店铺');
            }
            $where[] = ['shop_id', '=', $shopId];
        }

        $goodsId = array_unique(input('goods_id', []));

        if (!$goodsId) {
            $this->error('请选择商品');
        }

        \app\common\model\ShopPlatformGoods::where('shop_id', $shopId)->whereIn('goods_id', $goodsId)->useSoftDelete('delete_time', time())->delete();

        $this->success('删除成功');
    }*/

    public function edit()
    {

    }

    public function export()
    {

    }
}